<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.ww78.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: 快乐是福 <815856515@qq.com>
// +----------------------------------------------------------------------
// | QQ: 815856515
// +----------------------------------------------------------------------
// | 如需修改和引用，请保留此头部信息！

namespace Mz\Model;
class Qzone extends \Think\Model {
	public $msg;
	public $error;
	public function __construct($uin,$sid,$skey){
		$this->uin=$uin;
		$this->sid=$sid;
		$this->skey=$skey;
		$this->gtk=$this->getGTK($skey);
		$this->cookie='uin=o0'.$uin.'; skey='.$skey.';';
	}

	public function cpshuo($content,$richval='',$sname='',$lon='',$lat=''){
		$url='http://m.qzone.com/mood/publish_mood';
		$post="opr_type=publish_shuoshuo&res_uin=".$this->uin."&content={$content}&richval={$richval}&lat={$lat}&lon={$lon}&lbsid=&issyncweibo=0&is_winphone=2&format=json&source_name={$sname}&sid=".$this->sid;
		//$post='content='.$content.'++&sid='.$this->sid.'&lon='.$lon.'&lbsid=&res_uin='.$this->uin.'&richval='.urlencode($richval).'&source_name='.$sname.'&is_winphone=2&opr_type=publish_shuoshuo&format=json&issyncweibo=0&lat='.$lat;
		$result=$this->get_curl($url,$post);
        $json=json_decode($result,true);
        if(@array_key_exists('code',$json) && $json[code]==0){
            $this->msg[]=$this->uin.'发布说说成功[CP]';
			return '发布成功！';
        }else{
			$this->error[]=$this->uin.'发布说说失败[CP]，原因：'.$json[message];
			return;
		}
	}
	public function pcshuo($content,$richval=0,$sname=''){
		$url="http://user.qzone.qq.com/q/taotao/cgi-bin/emotion_cgi_publish_v6?g_tk=".$this->gtk;
		$post="syn_tweet_verson=1&paramstr=1&pic_template=";
		if($richval){
			$post.="&richtype=1&richval=,{$richval}&pic_bo=bgBuAAAAAAADACU! bgBuAAAAAAADACU!";
		}
		$post.="&special_url=&subrichtype=1&who=1&con={$content}&feedversion=1&ver=1&ugc_right=1&to_tweet=0&to_sign=0&hostuin=".$this->uin."&code_version=1&format=json&qzreferrer=http://user.qzone.qq.com/".$this->uin."/infocenter";
		$json=$this->get_curl($url,$post,0,$this->cookie);
		if($json){
			$arr=json_decode($json,true);
			$arr[feedinfo]='';
			if($arr[code]==0){
				$this->msg[]=$this->uin.'发布说说成功[PC]';
				return '发布成功！';
			}elseif($arr[code]==-3000){
				$this->skeyzt=1;
				$this->error[]='发布说说失败[PC]！原因:'.$arr[message];
				return;
			}elseif($arr[code]==-10045){
				$this->error[]=$this->uin.'发布说说失败[PC]！原因:'.$arr[message];
				return;
			}else{
				$this->error[]=$this->uin.'发布说说失败[PC]！原因'.$json;
				return;
			}
		}else{
			$this->error[]=$this->uin.'获取发布说说结果失败[PC]';
			return;
		}
	}
	public function shuo($do=0,$content,$image=0,$type=0,$sname=''){
		if(!$type && $image){
			if($pic=$this->get_curl($image)){
				$richval=$this->uploadimg($pic);
			}
		}else{
			$richval=$image;
		}
		if($do){
			return $this->pcshuo($content,$richval,$sname);
		}else{
			return $this->cpshuo($content,$richval,$sname);
		}
	
	}
	public function getGTK($skey){
        $len = strlen($skey);
        $hash = 5381;
        for($i = 0; $i < $len; $i++){
            $hash += ($hash << 5) + ord($skey[$i]);
        }
        return $hash & 0x7fffffff;//计算g_tk
    }
	public function uploadimg($image,$image_size=array()){
		$url="http://up.qzone.com/cgi-bin/upload/cgi_upload_pic_v2";
        $post="picture=".urlencode(base64_encode($image))."&base64=1&hd_height=".$image_size[1]."&hd_width=".$image_size[0]."&hd_quality=90&output_type=json&preupload=1&charset=utf-8&output_charset=utf-8&logintype=sid&Exif_CameraMaker=&Exif_CameraModel=&Exif_Time=&uin=".$this->uin."&sid=".$this->sid;
        $data=preg_replace("/\s/","",$this->get_curl($url,$post));
		preg_match('/_Callback\((.*)\);/',$data,$arr);
		$data=json_decode($arr[1],true);
        if($data && array_key_exists('filemd5',$data)){
			$this->msg[]='图片上传成功！';
			$post="output_type=json&preupload=2&md5=".$data['filemd5']."&filelen=".$data['filelen']."&batchid=".time().rand(100000,999999)."&currnum=0&uploadNum=1&uploadtime=".time()."&uploadtype=1&upload_hd=0&albumtype=7&big_style=1&op_src=15003&charset=utf-8&output_charset=utf-8&uin=".$this->uin."&sid=".$this->sid."&logintype=sid&refer=shuoshuo";
			$img=preg_replace("/\s/","",$this->get_curl($url,$post));
			preg_match('/_Callback\(\[(.*)\]\);/',$img,$arr);
			$data=json_decode($arr[1],true);
            if($data && array_key_exists('picinfo',$data)){
				if($data[picinfo][albumid]!=""){
					$this->msg[]='图片信息获取成功！';
					return "{$data['picinfo']['albumid']},{$data['picinfo']['lloc']},{$data['picinfo']['sloc']},{$data['picinfo']['type']},{$data['picinfo']['height']},{$data['picinfo']['width']},,,";
				}else{
					$this->msg[]='图片信息获取失败！';
					return;
				}
            }else{
                $this->error[]='图片信息获取失败！';
                return;
            }
		}else{
			$this->error[]='图片上传失败！原因：'.$data['msg'];
            return;
        }
	}
	public function get_curl($url,$post=0,$referer=0,$cookie=0,$header=0,$ua=0,$nobaody=0){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		$klsf[] = "Accept:application/json"; 
		$klsf[] = "Accept-Encoding:gzip,deflate,sdch"; 
		$klsf[] = "Accept-Language:zh-CN,zh;q=0.8"; 
		$klsf[] = "Connection:keep-alive"; 
		curl_setopt($ch, CURLOPT_HTTPHEADER, $klsf);
		if($post){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		}
		if($header){
			curl_setopt($ch, CURLOPT_HEADER, TRUE);
		}
		if($cookie){
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		}
		if($referer){
			if($referer==1){
				curl_setopt($ch, CURLOPT_REFERER, "http://m.qzone.com/infocenter?g_f=");
			}else{
				curl_setopt($ch, CURLOPT_REFERER, $referer);
			}
		}
		if($ua){
			curl_setopt($ch, CURLOPT_USERAGENT,$ua);
		}else{
			curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0');
		}
		if($nobaody){
			curl_setopt($ch, CURLOPT_NOBODY,1);//主要头部
			//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//跟随重定向
		}
		curl_setopt($ch, CURLOPT_ENCODING, "gzip");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		$ret = curl_exec($ch);
		curl_close($ch);
		return $ret;
	
	}
}